package wujinye.com.service.impl;

import wujinye.com.constant.Const;
import wujinye.com.dao.ConJoinDao;
import wujinye.com.dao.ConferenceDao;
import wujinye.com.dao.DeptDao;
import wujinye.com.dao.UserDao;
import wujinye.com.model.Conference;
import wujinye.com.service.MeetService;
import wujinye.com.utils.StrUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class MeetServiceImpl implements MeetService {

    private ConferenceDao conferenceDao = new ConferenceDao();

    private ConJoinDao conJoinDao = new ConJoinDao();

    private DeptDao deptDao = new DeptDao();

    private UserDao userDao = new UserDao();

    @Override
    public List<Conference> findMeetingList(String search, String deptName, int page) {
        List<Conference> list = conferenceDao.findMeetingList(search, deptName, page);
        // 每次获取时，比较会议时间与现在时间，确定会议状态
        for (Conference c : list) {
            if (c.getStatus() != 3) {
                compareTime(c);
            }
        }
        return list;
    }

    @Override
    public long getTotalMeetings(String search, String deptName) {
        return conferenceDao.getTotalMeetings(search, deptName);
    }

    @Override
    public Conference findMeetingById(Integer mId) {
        Conference conference = conferenceDao.findMeetingById(mId);
        if (conference.getStatus() != 3) {
            compareTime(conference);
        }
        return conference;
    }

    @Override
    public void compareTime(Conference c) {
        Date now = new Date();
        long nowTime = now.getTime();
        try {
            // 每次获取时，比较会议时间与现在时间，确定会议状态
            String d = c.getPublishDate().toString() + " " + c.getStartTime().toString();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(d);

            long startTime = date.getTime();
            long endTime = startTime + Const.MEETING_MILLIS_TIME;
            if (nowTime > startTime && nowTime < endTime) {
                c.setStatus(1);
                conferenceDao.setStatus(1, c.getId());
            }
            if (nowTime > endTime) {
                c.setStatus(2);
                conferenceDao.setStatus(2, c.getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public long getTotalPerson(Integer mId) {
        return conJoinDao.getTotalPerson(mId);
    }

    @Override
    public long getTotalAbsence(Integer mId) {
        return conJoinDao.getTotalAbsence(mId);
    }

    @Override
    public Object[] isCome(Integer uId, Integer mId) {
        return conJoinDao.isCome(uId, mId);

    }

    @Override
    public void cancelMeeting(Integer mId) {
        conferenceDao.cancelMeeting(mId);
    }

    @Override
    public void joinMeeting(Integer mId, Integer uId) {
        conJoinDao.joinMeeting(mId, uId);
    }

    @Override
    public void addMeeting(Integer userId, String deptId, String title, String content, String startTime) {
        Conference conference = new Conference();
        Integer deptID = StrUtil.stringToInteger(deptId);
        Object[] obj = deptDao.findDeptNameById(deptID);
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat time = new SimpleDateFormat("HH:mm");
        String format = f.format(new Date());
        try {
            conference.setId(null);
            if (null != obj) {
                conference.setDeptName(obj[0].toString());
            }
            conference.setDeptId(deptID);
            conference.setTitle(title);
            conference.setContent(content);
            conference.setPublishDate(f.parse(format));
            conference.setStartTime(time.parse(startTime));
            conference.setStatus(0);
            conference.setUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Integer cId = StrUtil.stringToInteger(conferenceDao.addMeeting(conference).toString());
        // 通过部门id，查找该部门下的员工id
        List<Map<String, Object>> userIdList = userDao.getUserId(deptID);
        Object[][] params = null;
        if (null != userIdList) {
            params = new Object[userIdList.size()][];
            int count = 0;
            for (Map<String, Object> map : userIdList) {
                Integer id = StrUtil.stringToInteger(map.get("id").toString());
                if (userId.equals(id)) {
                    // 该员工为会议发布者，默认status=1，确认参加
                    params[count] = new Object[]{null, id, cId, 1};
                } else {
                    // 该员工不为发布者，默认status=0，未确认参加
                    params[count] = new Object[]{null, id, cId, 0};
                }
                count++;
            }
            conJoinDao.setStatus(params);
        } else {
            System.out.println("部门不存在或者部门里面没有员工");
        }
    }
}
